﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ServiceProxy
{
    /// <summary>
    /// 方案同步服务的代理管理类
    /// </summary>
    public static class SyncServiceProxyManager
    {
        public static List<SyncServiceProxy> Proxies { get; private set; }

        static SyncServiceProxyManager()
        {
            Proxies = new List<SyncServiceProxy>();
        }

        /// <summary>
        /// 初始化客户端代理对象。
        /// 如果代理对象已经存在，但是状态不是Connected也会重新初始化。
        /// 已经是Connected状态的不会初始化
        /// </summary>
        public static void Init()
        {
            if (Proxies.Count != 0)
            {
                throw new ProxyListNotEmptyException("代理列表里存在代理对象");
            }

            List<Configuration.Client.ServiceConfig> serviceConfigList = new List<Configuration.Client.ServiceConfig>();

            serviceConfigList.Add(Configuration.Client.Config.Host.GetServiceConfig("SyncService"));

            if (Configuration.Client.Config.SpareHostList != null)
            {
                for (int index = 0; index < Configuration.Client.Config.SpareHostList.Count; index++)
                {
                    serviceConfigList.Add(Configuration.Client.Config.SpareHostList[index].GetServiceConfig("SyncService"));
                }
            }

            for (int index = 0; index < serviceConfigList.Count; index++)
            {
                Proxies.Add(new SyncServiceProxy(serviceConfigList[index]));
            }

            // 打开第一个客户端代理
            if(Proxies.Count >0)
            {
                Proxies[0].Open();
            }
        }

        /// <summary>
        /// 释放所有的客户端代理对象
        /// </summary>
        public static void Release()
        {
            for (int index = 0; index < Proxies.Count; index++)
            {
                Proxies[index].Close();
                Proxies[index] = null;
            }
            Proxies.Clear();
        }

        /// <summary>
        /// 告知管理器Proxy对象出错了。
        /// </summary>
        /// <param name="proxy"></param>
        public static void Error(SyncServiceProxy proxy)
        {
            proxy.Disconnect();
        }

        /// <summary>
        /// 获取一个可用的同步客户端代理
        /// </summary>
        /// <returns></returns>
        public static SyncServiceProxy GetAvailableProxy()
        {
            for (int index = 0; index < Proxies.Count; index++)
            {
                if(Proxies[index].State == ServiceProxyState.Connected)
                {
                    return Proxies[index];
                }
                else if(Proxies[index].State == ServiceProxyState.Inited)
                {
                    Proxies[index].Open();
                    if (Proxies[index].State == ServiceProxyState.Connected)
                    {
                        return Proxies[index];
                    }
                }
            }
            return null;
        }
    }
}
